package com.situ.company.score.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.situ.company.score.model.ScoreModel;
import com.situ.company.score.service.ScoreService;

@WebServlet("/score")
public class ScoreServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        switch (action) {
            case "list":
                try {
                    List<ScoreModel> list = selectList(req);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("data", list);
                    map.put("count", list.size());
                    map.put("code", 0);
                    JSONObject json = new JSONObject(map);
                    String jsonString = json.toString();
                    resp.getWriter().print(jsonString);
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "add":
                try {
                    int count = insert(req);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("code", count);
                    JSONObject json = new JSONObject(map);
                    String jsonString = json.toString();
                    resp.getWriter().print(jsonString);
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "del":
                try {
                    int count = delete(req);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("code", count);
                    JSONObject json = new JSONObject(map);
                    String jsonString = json.toString();
                    resp.getWriter().print(jsonString);
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "sel":
                try {
                    ScoreModel dbm = selectByCode(req);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("code", dbm);
                    JSONObject json = new JSONObject(map);
                    String jsonString = json.toString();
                    resp.getWriter().print(jsonString);
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "upd":
                try {
                    int count = update(req);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("code", count);
                    JSONObject json = new JSONObject(map);
                    String jsonString = json.toString();
                    resp.getWriter().print(jsonString);
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private ScoreService service = new ScoreService();

    public int update(HttpServletRequest req) throws ClassNotFoundException, SQLException {
        String code_emp = req.getParameter("code_emp");
        String code_pro = req.getParameter("code_pro");
        String score = req.getParameter("score");
        ScoreModel model = new ScoreModel();
        model.setCode_emp(code_emp);
        model.setCode_pro(code_pro);
        model.setScore(score);
        return service.update(model);
    }

    public ScoreModel selectByCode(HttpServletRequest req) throws ClassNotFoundException, SQLException {
        String code_emp = req.getParameter("code_emp");
        String code_pro = req.getParameter("code_pro");
        ScoreModel model = new ScoreModel();
        model.setCode_emp(code_emp);
        model.setCode_pro(code_pro);
        return service.selectByCode(model);
    }

    public int delete(HttpServletRequest req) throws ClassNotFoundException, SQLException {
        String code_emp = req.getParameter("code_emp");
        String code_pro = req.getParameter("code_pro");
        ScoreModel model = new ScoreModel();
        model.setCode_emp(code_emp);
        model.setCode_pro(code_pro);
        return service.delete(model);
    }

    public List<ScoreModel> selectList(HttpServletRequest req) throws ClassNotFoundException, SQLException {
        String code_emp = req.getParameter("code_emp");
        String code_pro = req.getParameter("code_pro");
        code_emp = code_emp == null ? "" : code_emp;
        code_pro = code_pro == null ? "" : code_pro;
        ScoreModel model = new ScoreModel();
        model.setCode_emp(code_emp);
        model.setCode_pro(code_pro);
        return service.selectList(model);
    }

    public int insert(HttpServletRequest req) throws ClassNotFoundException, SQLException {
        String code_emp = req.getParameter("code_emp");
        String code_pro = req.getParameter("code_pro");
        String score = req.getParameter("score");
        ScoreModel model = new ScoreModel();
        model.setCode_emp(code_emp);
        model.setCode_pro(code_pro);
        model.setScore(score);
        return service.insert(model);
    }
}
